# -*- ecoding: utf-8 -*-
# @Function: 
# @Author: Noaman
# @Time: 2021/10/26 19:36

'''
此脚本用来检查生产环境现场，箱子的一些位置信息及位置上箱子的信息；
由于查询的数据量很多，需要在生产环境结束，没有业务任务且机器人身上没有箱子的时候执行，否则可能会导致ess很卡
一旦出现箱子有位置，但是工作位没箱子的情况，需要及时处理，不然mc一旦分配了该库位，机器人到了目的地会awaiting，时间长，导致巷道堵塞
脚本涉及的数据很多，出结果需要两分钟左右（50000个库位为参考）
'''
import requests
import pymysql
from config import ip_port



# 查询所有料箱的接口   swagger没有
queryContainers_url = f'http://{ip_port}/ess-api/model/queryModelByType?modelType=container'
# 查询所有位置的接口
queryLocation_url = f'http://{ip_port}/ess-api/model/queryModelByType?modelType=location'
# 查询料箱位置地址
queryModelByCode_url = f'http://{ip_port}/ess-api/model/queryModelByCode'
# 请求头
headers = {'Content-Type': 'application/json', 'accept': 'application/json'}

ip = 'mysql-cluster.primary.dev.com'
user = 'root'
password = 'gouzi@520'
db = 'iwms_core_test'


# 得到都有位置的容器list（容器有位置）
def getContainersOnLocation():
    binCodes = []
    response = requests.get(queryContainers_url, headers=headers)
    result = response.json()
    containers = result.get('data').get('container')
    for container in containers:
        code = container.get('code')
        positionCode = container.get('positionCode')
        if positionCode == '':
            continue
        else:
            binCodes.append(code)
    return binCodes


# 得到都有工作位的容器list（工作位上有容器）
def getLocationWithContainers():
    binCodes = []
    response = requests.get(queryLocation_url, headers=headers)
    result = response.json()
    locations = result.get('data').get('location')
    for location in locations:
        locationTypeCode = location.get('locationTypeCode')
        if locationTypeCode == 'LT_SHELF_STORAGE' or locationTypeCode == 'HIGH_SHELF_STORAGE':
            containerCode = location.get('containerCode')
            if containerCode == '':
                continue
            else:
                binCodes.append(containerCode)
        else:
            continue
    return binCodes


# 得到箱子有位置，但是工作位没有箱子的list
def getlocationNotContainerAndContainerWithLocation(containersOnLocations, locationWithContainers):
    binCodes = []
    for container in containersOnLocations:
        if container not in locationWithContainers:
            binCodes.append(container)
        else:
            continue
    return binCodes


# 得到工作位有容器，但是箱子没有位置的list（基本不会出现，检查下）
def getlocationWithContainerAndContainerNotLocation(containersOnLocations, locationWithContainers):
    binCodes = []
    for container in locationWithContainers:
        if container not in containersOnLocations:
            binCodes.append(container)
        else:
            continue
    return binCodes


# 得到容器占用多个工作位的list
def getContainerWithMultipleLocations(locationWithContainers):
    binCodes = []
    containers = []
    for container in locationWithContainers:
        if container not in containers:
            containers.append(container)
        else:
            binCodes.append(container)
    return binCodes


# 得到所有有库存的箱子，包括0库存
# def getAllWithStockContainers():
#     connect = pymysql.connect(host=ip, user=user, password=password, database=db)
#     cursor = connect.cursor()
#     sql = 'SELECT container_code FROM ks_stock'
#     cursor.execute(sql)
#     result = cursor.fetchall()
#     cursor.close()
#     connect.close()
#     containers = []
#     for i in range(len(result)):
#         container = result[i][0]
#         containers.append(container)
#     return containers


# 得到essp占有位置，但是在iwms库存表里面没有数据的箱子
def getSurplusContainers(containers, binCodes):
    bin_Codes = []
    for container in binCodes:
        if container not in containers:
            bin_Codes.append(container)
        else:
            continue
    return bin_Codes


# 得到所有库存大于0的箱子
def getAllNotEmptyContainers():
    connect = pymysql.connect(host=ip, user=user, password=password, database=db)
    cursor = connect.cursor()
    sql = 'SELECT container_code FROM ks_stock WHERE qty_actual >0;'
    cursor.execute(sql)
    result = cursor.fetchall()
    cursor.close()
    connect.close()
    containers = []
    for i in range(len(result)):
        container = result[i][0]
        containers.append(container)
    return containers


# 得到有库存的，但是没位置的箱子(性能太差，弃用）
# def getNotEmptyAndNotLocationContainers(containers):
#     binCodes = []
#     for container in containers:
#         url = queryModelByCode_url + '?modelCode=' + container
#         response = requests.get(url=url, headers=headers)
#         positionCode = response.json().get('data').get('container').get('positionCode')
#         if positionCode == '':
#             binCodes.append(container)
#         else:
#             continue
#     return binCodes

# 得到有库存的，但是没位置的箱子
def getNotEmptyAndNotLocationContainers(notEmptyContainers, containersOnLocations):
    binCodes = []
    for container in notEmptyContainers:
        if container not in containersOnLocations:
            binCodes.append(container)
        else:
            continue
    return binCodes


def repeat_containers():
    containersOnLocations = getContainersOnLocation()
    locationWithContainers = getLocationWithContainers()
    locationWithContainerAndContainerNotLocation = getlocationWithContainerAndContainerNotLocation(
        containersOnLocations, locationWithContainers)
    containerWithMultipleLocations = getContainerWithMultipleLocations(locationWithContainers)

    return [
        ("工作位有箱子，但是箱子没位置的列表如下: (现场操作员将料箱拿下之后，将容器离场)", locationWithContainerAndContainerNotLocation),
        ("箱子占有多个工作位的列表如下:", containerWithMultipleLocations)
    ]

